package com.fable.sp.core.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.fable.sp.core.enums.ContentType;
import com.google.common.hash.Hashing;
import org.springframework.util.StringUtils;

import javax.persistence.*;

/**
 * .
 *
 * @author stormning on 16/7/30.
 */
@Entity
@Table(name = "sp_layout_item_content", indexes = {@Index(columnList = "layout_id"), @Index(columnList = "layout_item_id")})
public class LayoutItemContent extends StyleEntity implements Comparable<LayoutItemContent> {

    @Column(name = "layout_id")
    private long layoutId;

    @Column(name = "layout_item_id")
    private long layoutItemId;

    @Column(name = "content_id")
    private long contentId;

    @Column(name = "content_type")
    private ContentType contentType;

    @Column(name = "weight")
    private int weight;

    @Transient
    @JSONField(serialize = false)
    private String content;

    @Transient
    @JSONField(serialize = false)
    private Object instance;
    private String mode;

    public long getLayoutItemId() {
        return layoutItemId;
    }

    public void setLayoutItemId(long layoutItemId) {
        this.layoutItemId = layoutItemId;
    }

    public long getContentId() {
        return contentId;
    }

    public void setContentId(long contentId) {
        this.contentId = contentId;
    }

    public ContentType getContentType() {
        return contentType;
    }

    public void setContentType(ContentType contentType) {
        this.contentType = contentType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public long getLayoutId() {
        return layoutId;
    }

    public void setLayoutId(long layoutId) {
        this.layoutId = layoutId;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public Object getInstance() {
        return instance;
    }

    public void setInstance(Object instance) {
        this.instance = instance;
    }

    @Override
    public int compareTo(LayoutItemContent o) {
        return getWeight() - o.getWeight();
    }

    @Override
    public int hashCode() {
        return Hashing.sipHash24().hashLong(getContentId()).asInt();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof LayoutItemContent) {
            return ((LayoutItemContent) obj).getContentId() == getContentId();
        } else {
            return super.equals(obj);
        }
    }

    public String getMode() {
        return StringUtils.hasText(mode) ? mode : "edit";
    }

    public void setMode(String mode) {
        this.mode = mode;
    }
}
